package com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.sjcj.common.utils.*;
import com.css.zfzx.sjcj.modules.boundary.repository.BoundaryNativeRepository;
import com.css.zfzx.sjcj.modules.boundary.repository.entity.BoundaryEntity;
import com.css.zfzx.sjcj.modules.boundary.viewobjects.BoundaryQueryParams;
import com.css.zfzx.sjcj.modules.qhmapfiles.repository.QhMapFilesImport;
import com.css.zfzx.sjcj.modules.qhmapfiles.repository.entity.QhMapFilesEntity;
import com.css.zfzx.sjcj.modules.qhmapfiles.viewobjects.QhMapFilesQueryParams;
import com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.repository.QhRiskLevelMapPolygonImport;
import com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.repository.QhRiskLevelMapPolygonNativeRepository;
import com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.repository.QhRiskLevelMapPolygonRepository;
import com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.repository.entity.QhRiskLevelMapPolygonEntity;
import com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.service.QhRiskLevelMapPolygonService;
import com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.viewobjects.QhRiskLevelMapPolygonQueryParams;
import com.css.zfzx.sjcj.modules.qhrisklevelmappolygon.viewobjects.QhRiskLevelMapPolygonVo;
import lombok.extern.slf4j.Slf4j;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.opengis.feature.simple.SimpleFeature;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author andy
 * @date 2021-01-26
 */
@Service
@Slf4j
public class QhRiskLevelMapPolygonServiceImpl implements QhRiskLevelMapPolygonService {

	@Autowired
    private QhRiskLevelMapPolygonRepository qhRiskLevelMapPolygonRepository;
    @Autowired
    private QhRiskLevelMapPolygonNativeRepository qhRiskLevelMapPolygonNativeRepository;
    @Autowired
    private QhRiskLevelMapPolygonImport qhRiskLevelMapPolygonImport;
    @Autowired
    private QhMapFilesImport qhMapFilesImport;//上传文件管理

    @Override
    public JSONObject queryQhRiskLevelMapPolygons(QhRiskLevelMapPolygonQueryParams queryParams, int curPage, int pageSize) {
        Page<QhRiskLevelMapPolygonEntity> qhRiskLevelMapPolygonPage = this.qhRiskLevelMapPolygonNativeRepository.queryQhRiskLevelMapPolygons(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhRiskLevelMapPolygonPage);
        return jsonObject;
    }

    @Override
    public JSONObject queryQhRiskLevelMapPolygonsByFileName(QhRiskLevelMapPolygonQueryParams queryParams, int curPage, int pageSize, String sort, String order) {
        if (!PlatformObjectUtils.isEmpty(sort) && sort.length() > 0) {
            sort = CheckObjFields.propertyChange(sort);
        }
        Page<QhRiskLevelMapPolygonEntity> qhRiskLevelMapPolygonPage = this.qhRiskLevelMapPolygonNativeRepository.queryQhRiskLevelMapPolygonsByFileNameJDBC(queryParams, curPage, pageSize, sort, order);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(qhRiskLevelMapPolygonPage);
        return jsonObject;
    }

    @Override
    public QhRiskLevelMapPolygonEntity getQhRiskLevelMapPolygon(String id) {
        QhRiskLevelMapPolygonEntity qhRiskLevelMapPolygon = this.qhRiskLevelMapPolygonRepository.findById(id).orElse(null);
         return qhRiskLevelMapPolygon;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhRiskLevelMapPolygonEntity saveQhRiskLevelMapPolygon(QhRiskLevelMapPolygonEntity qhRiskLevelMapPolygon) {
        String uuid = UUIDGenerator.getUUID();
        qhRiskLevelMapPolygon.setUuid(uuid);
        qhRiskLevelMapPolygon.setCreateUser(PlatformSessionUtils.getUserId());
        qhRiskLevelMapPolygon.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.qhRiskLevelMapPolygonRepository.save(qhRiskLevelMapPolygon);
        return qhRiskLevelMapPolygon;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public QhRiskLevelMapPolygonEntity updateQhRiskLevelMapPolygon(QhRiskLevelMapPolygonEntity qhRiskLevelMapPolygon) {
        qhRiskLevelMapPolygon.setUpdateUser(PlatformSessionUtils.getUserId());
        qhRiskLevelMapPolygon.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        String id = qhRiskLevelMapPolygon.getUuid();
        QhRiskLevelMapPolygonEntity one = qhRiskLevelMapPolygonRepository.findById(id).get();
        BeanUtil.copyPropertiesIgnoreNull(qhRiskLevelMapPolygon, one);
        this.qhRiskLevelMapPolygonRepository.save(one);
        return qhRiskLevelMapPolygon;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhRiskLevelMaps(List<String> ids) {
        List<QhRiskLevelMapPolygonEntity> qhRiskLevelMapPolygonList = this.qhRiskLevelMapPolygonRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(qhRiskLevelMapPolygonList) && qhRiskLevelMapPolygonList.size() > 0) {
            for(QhRiskLevelMapPolygonEntity qhRiskLevelMapPolygon : qhRiskLevelMapPolygonList) {
                this.qhRiskLevelMapPolygonRepository.delete(qhRiskLevelMapPolygon);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQhRiskLevelMapPolygonsByFileName(List<String> fileNames) {
        for (int i = 0; i < fileNames.size(); i++) {
            QhRiskLevelMapPolygonQueryParams queryParams = new QhRiskLevelMapPolygonQueryParams();
            queryParams.setAffiliatedFileName(fileNames.get(i));
            System.out.println(new Date()+"----开始删除." + fileNames.get(i));
            List<String> uuidList = this.qhRiskLevelMapPolygonNativeRepository.listQhRiskLevelMapPolygonsUuidsByJDBC(queryParams);
            if(!PlatformObjectUtils.isEmpty(uuidList) && uuidList.size() > 0) {
                System.out.println(new Date()+"----开始逻辑删除." + uuidList.size() + "条数据");
                //物理删
                //qhRiskLevelMapPolygonImport.sqlBatchDeleteEntities(qhRiskLevelMapPolygonList);
                //逻辑删
                qhRiskLevelMapPolygonImport.newSqlBatchLogicDeleteEntities(uuidList);
                System.out.println(new Date()+"----结束逻辑删除." + uuidList.size() + "条数据");
            }
            //查询上传文件表
            QhMapFilesQueryParams qhMapFilesQueryParams = new QhMapFilesQueryParams();
            qhMapFilesQueryParams.setAffiliatedFileName(fileNames.get(i));
            qhMapFilesQueryParams.setType("RLMPolygon_");//上传类型（PED_,RLM_,RLMPolygon_,SCR_）
            qhMapFilesQueryParams.setIsDelete("0");//未删除
            List<QhMapFilesEntity> list = qhMapFilesImport.searchMapFiles(qhMapFilesQueryParams);
            if(!PlatformObjectUtils.isEmpty(list) && list.size() > 0) {
                //逻辑删--上传文件表
                qhMapFilesImport.sqlBatchLogicDeleteEntities(list);
            }
            System.out.println(new Date()+"----结束删除." + fileNames.get(i));
        }
    }

    @Override
    public JSONObject getQhRiskLevelMapPolygons(QhRiskLevelMapPolygonQueryParams queryParams) {
        //List<QhRiskLevelMapPolygonEntity> qhRiskLevelMapPolygons = this.qhRiskLevelMapPolygonNativeRepository.listQhRiskLevelMapPolygons(queryParams);
        List<QhRiskLevelMapPolygonEntity> qhRiskLevelMapPolygons = this.qhRiskLevelMapPolygonNativeRepository.listQhRiskLevelMapPolygonsByJDBC(queryParams);
        for (QhRiskLevelMapPolygonEntity rlmEntity : qhRiskLevelMapPolygons) {
            String coords = rlmEntity.getCoordinate();
            if (coords == null) continue;
            coords = coords.toLowerCase();
            if (coords.indexOf("multipolygon") != -1) {
                coords = coords.substring(coords.indexOf("(((") + 3, coords.lastIndexOf(")))"));
                coords = coords.replace(")), ((", "|");
                coords = coords.replace("), (", "#");
                rlmEntity.setCoordinate(coords);
            } else if (coords.indexOf("polygon") != -1) {
                String sp = coords.indexOf("),(") != -1 ? "),(" : (coords.indexOf("), (") != -1 ? "), (" : "");
                String oldWkt = coords, coordinate = "";
                if ("".equals(sp)) {
                    oldWkt = oldWkt.substring(oldWkt.indexOf("(") + 2, oldWkt.lastIndexOf("))"));
                    String[] coordinates = oldWkt.split(",");
                    for (int i = 0; i < coordinates.length; i++) {
                        String[] _coords = coordinates[i].split(" ");
                        coordinate += _coords[0] + " " + _coords[1] + ",";
                    }
                    coordinate = coordinate.substring(0, coordinate.length() - 1);
                    rlmEntity.setCoordinate(coordinate);
                } else {
                    oldWkt = oldWkt.replace(sp, "#"); sp = "#";
                    String[] coordinates = oldWkt.split(sp); String tempCoordinate = "";
                    for (int i = 0; i < coordinates.length; i++) {
                        tempCoordinate = coordinates[i];
                        String[] _coords = tempCoordinate.split(","), __coords = null;
                        for (int j = 0; j < _coords.length; j++) {
                            __coords = _coords[j].split(" ");
                            coordinate += __coords[0] + " " + __coords[1] + ",";
                        }
                        coordinate = coordinate.substring(0, coordinate.length() - 1) + "|";
                    }
                    rlmEntity.setCoordinate(coordinate);
                }
            }
            //scrEntity.setCoordinates(ProjectTransUtil.parseWktPolygon(scrEntity.getCoordinates()));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", qhRiskLevelMapPolygons);
        return jsonObject;
    }

    @Override
    public void saveOrUpdate(QhRiskLevelMapPolygonEntity rlmEntity) {
        QhRiskLevelMapPolygonQueryParams queryParams = new QhRiskLevelMapPolygonQueryParams();
        queryParams.setProvince(rlmEntity.getProvince());
        queryParams.setCity(rlmEntity.getCity());
        queryParams.setArea(rlmEntity.getArea());
        List<QhRiskLevelMapPolygonEntity> rlmEntities = qhRiskLevelMapPolygonNativeRepository.listQhRiskLevelMapPolygons(queryParams);
        // 若查到数据为空，则新增
        if (CollectionUtils.isEmpty(rlmEntities)) {
            this.saveQhRiskLevelMapPolygon(rlmEntity);
        } else {
            if (rlmEntities.size() == 1) {
                QhRiskLevelMapPolygonEntity sourceProvinceEntity = rlmEntities.get(0);
                BeanUtil.copyPropertiesIgnoreNull(rlmEntity, sourceProvinceEntity);
                this.updateQhRiskLevelMapPolygon(sourceProvinceEntity);
            } else {
                throw new RuntimeException("库中该数据最多一条，请管理员清理数据库冗余数据！");
            }
        }
    }

    @Override
    public String judgeFileParams(String[] datas) {
        String str = "";
        Set<String> set = new HashSet<String>(Arrays.asList(datas));
        if(!set.contains("A")){
            str += "A,";
        }
        if(!set.contains("B")){
            str += "B,";
        }
        if(!set.contains("C")){
            str += "C,";
        }
        return str;
    }

    private String judgeFieldParams(List<List<Object>> list) {
        String str = "";
        for (int i = 0; i < list.size(); i++) {
            List<Object> values = list.get(i);
            //for (int j = 0; j < values.size(); j++) {
            for (int j = 0; j < 2; j++) {
                if ("".equals(values.get(j))) {
                    str += "第 " + i + " 条数据第 " + j + " 个字段数据为空.<br/>";
                }
            }
        }
        return str;
    }

    @Override
    public int lookUpQhRiskLevelMapPolygons(QhRiskLevelMapPolygonQueryParams queryParams) {
        int count = this.qhRiskLevelMapPolygonNativeRepository.lookUpQhRiskLevelMapPolygons(queryParams);
        return count;
    }

    //保存到上传文件管理表
    private void saveMapFiles(QhMapFilesEntity qhMapFilesEntity) throws Exception {
        List<QhMapFilesEntity> list = new ArrayList<QhMapFilesEntity>();
        list.add(qhMapFilesEntity);
        String affiliatedFileName = qhMapFilesEntity.getAffiliatedFileName();
        String province = qhMapFilesEntity.getProvince();
        String city = qhMapFilesEntity.getCity();
        String area = qhMapFilesEntity.getArea();
        //逻辑删除未保存的文件
        deleteMapFileByParams(affiliatedFileName, province, city, area);
        //保存
        qhMapFilesImport.sqlBatchSaveEntities(list);
    }

    //逻辑删除未保存的文件
    private void deleteMapFileByParams(String affiliatedFileName, String province, String city, String area) {
        //查询上传文件表
        QhMapFilesQueryParams qhMapFilesQueryParams = new QhMapFilesQueryParams();
        qhMapFilesQueryParams.setProvince(province);
        qhMapFilesQueryParams.setCity(city);
        qhMapFilesQueryParams.setArea(area);
        //qhMapFilesQueryParams.setAffiliatedFileName(affiliatedFileName);
        qhMapFilesQueryParams.setType("RLMPolygon_");//上传类型（PED_,RLM_,RLMPolygon_,SCR_）
        //查询文件（未删除）
        List<QhMapFilesEntity> list = qhMapFilesImport.searchMapFilesUnSaved(qhMapFilesQueryParams);
        if(!PlatformObjectUtils.isEmpty(list) && list.size() > 0) {
            //逻辑删--上传文件表
            qhMapFilesImport.sqlBatchLogicDeleteEntities(list);
        }
    }

    //校验shp文件
    public String checkSHPFiles(String filePath, String province, String city, String area) {
        System.out.println(new Date()+"-------------------1.开始readSHPFile");
        System.out.println(new Date()+"-------------------2.开始校验");
        //获取省市县所在区域字符串
        String boundariesCoordinate = getDistrictBoundaryCoordinate(province, city, area);
        //读取shp文件并校验--type上传类型（PED_,RLM_,RLMPolygon_,SCR_）
        List<Object> list = VectorDataUtil.readSHPFileCheck(filePath, "UTF-8","RLMPolygon_", boundariesCoordinate);
        String codeType = (String) list.get(0);//编码类型（0：UTF-8，1：GBK）
        int dataLength = (int) list.get(1);//数据长度
        String checkResult = (String) list.get(2);//校验结果
        //编码类型（0：UTF-8，1：GBK）
        if (codeType.equals("UTF-8")) {
            codeType = "0";
        }else{
            codeType = "1";
        }
        if(checkResult.equals("校验成功")){
            System.out.println(new Date()+"-------------------1.完成校验是否有乱码");
            System.out.println(new Date()+"-------------------2.完成校验字段空值");
            System.out.println(new Date()+"-------------------3.完成校验空间分辨率是否为6秒");
            System.out.println(new Date()+"-----------校验成功.");
        }else{
            System.out.println(new Date()+"-----------校验失败."+checkResult);
            return checkResult;
        }
        System.out.println(new Date()+"-----------3.结束校验");

        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length() - 4);
        // 返回的消息
        StringBuilder returnMsg = new StringBuilder();
        //上传文件信息
        QhMapFilesEntity qhMapFilesEntity = new QhMapFilesEntity();
        qhMapFilesEntity.setFilepath(filePath);
        qhMapFilesEntity.setProvince(province);
        qhMapFilesEntity.setCity(city);
        qhMapFilesEntity.setArea(area);
        qhMapFilesEntity.setAffiliatedFileName(fileName);
        qhMapFilesEntity.setIsSave("0");
        qhMapFilesEntity.setIsDelete("0");
        qhMapFilesEntity.setType("RLMPolygon_");//类型
        qhMapFilesEntity.setPostfix("shp");
        qhMapFilesEntity.setQualityStatus("1");//上传省市级类型（0：省，1：市）
        qhMapFilesEntity.setCodeType(codeType);//编码类型（0：UTF-8，1：GBK）
        qhMapFilesEntity.setDataLength(dataLength);//数据长度
        try {
            System.out.println(new Date()+"-------------------4.开始保存");
            //保存到上传文件管理表
            this.saveMapFiles(qhMapFilesEntity);
            System.out.println(new Date()+"-------------------5.结束保存");
            returnMsg.append("等待导入" + dataLength + "条数据.请第二天查询上传内容.");
            return returnMsg.toString();
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return "插入或更新数据出现错误: <br/>" + e.getMessage();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "数据导入失败, 服务器内部出现异常!";
        }
    }

    //保存--zyt
    @Override
    public String newImportShapeFile(String filePath, String province, String city, String area, String createUser,String codeType,int dataLength) {
        // 返回的消息
        StringBuilder returnMsg = new StringBuilder();
        try {
            System.out.println(new Date()+"-------------------1.开始保存");
            //保存--zyt
            int count = this.newSaveData(filePath,province,city,area, codeType, dataLength,createUser);
            if(count==dataLength){
                System.out.println(new Date()+"-------------------2.结束保存");
                returnMsg.append("成功导入" + count + "条数据.");
            }else{
                System.out.println(new Date()+"-------------------2.结束保存");
                returnMsg.append("导入出现异常，仅导入" + count + "条数据.");
            }
            return returnMsg.toString();
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return "插入或更新数据出现错误: <br/>" + e.getMessage();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "数据导入失败, 服务器内部出现异常!";
        }
    }

    //保存--zyt
    private int newSaveData(String filePath, String province, String city, String area, String codeType, int dataLength, String createUser) {
        int count = qhRiskLevelMapPolygonImport.newSqlBatchSaveEntities(filePath,province,city,area,codeType,dataLength,createUser);

        return count;
    }

    @Autowired
    private BoundaryNativeRepository boundaryNativeRepository;
    //获取省市县所在区域字符串
    public String getDistrictBoundaryCoordinate(String province, String city, String area){
        String coordinate = "";
        BoundaryQueryParams queryParams = new BoundaryQueryParams();
        queryParams.setType("1");//1: 行政区划 2: 普查区划
        if(PlatformObjectUtils.isEmpty(area)){//市
            queryParams.setLevel("1");//0: 省 1: 市 2: 县
            queryParams.setName(city);
        }else{//县
            queryParams.setLevel("2");//0: 省 1: 市 2: 县
            queryParams.setName(area);
        }
        queryParams.setProvince(province);
        queryParams.setCity(city);
        queryParams.setArea(area);
        //查询省市县所在区域边界列表
        List<BoundaryEntity> boundaryEntities = this.boundaryNativeRepository.newListBoundaries(queryParams);
        if (boundaryEntities.size() == 1) {
            BoundaryEntity boundaryEntity = boundaryEntities.get(0);
            coordinate = boundaryEntity.getCoordinate();

        } else if (boundaryEntities.size() == 0) {
            System.out.println("NoDistrictBoundaryData");
        } else {
            System.out.println("DistrictBoundaryDataRedundance");
        }
        return coordinate;
    }

    //校验GDB文件
    public String checkGDBFiles(String filePath, String province, String city, String area) {
        System.out.println(new Date()+"-------------------1.开始readGDBFile");
        System.out.println(new Date()+"-------------------2.开始校验");
        //获取省市县所在区域字符串
        String boundariesCoordinate = getDistrictBoundaryCoordinate(province, city, area);
        //读取GDB文件并校验--type上传类型（PED_,RLM_,RLMPolygon_,SCR_）
        List<Object> list = GdbDataUtil.readGDBFileCheck(filePath, "UTF-8","RLMPolygon_", boundariesCoordinate);
        String codeType = (String) list.get(0);//编码类型（0：UTF-8，1：GBK）
        int dataLength = (int) list.get(1);//数据长度
        String checkResult = (String) list.get(2);//校验结果
        //编码类型（0：UTF-8，1：GBK）
        if (codeType.equals("UTF-8")) {
            codeType = "0";
        }else{
            codeType = "1";
        }
        if(checkResult.equals("校验成功")){
            System.out.println(new Date()+"-------------------1.完成校验是否有乱码");
            System.out.println(new Date()+"-------------------2.完成校验字段空值");
            System.out.println(new Date()+"-------------------3.完成校验空间分辨率是否为6秒");
            System.out.println(new Date()+"-----------校验成功.");
        }else{
            System.out.println(new Date()+"-----------校验失败."+checkResult);
            return checkResult;
        }
        System.out.println(new Date()+"-----------3.结束校验");

        //String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length() - 4);
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
        // 返回的消息
        StringBuilder returnMsg = new StringBuilder();
        //上传文件信息
        QhMapFilesEntity qhMapFilesEntity = new QhMapFilesEntity();
        qhMapFilesEntity.setFilepath(filePath);
        qhMapFilesEntity.setProvince(province);
        qhMapFilesEntity.setCity(city);
        qhMapFilesEntity.setArea(area);
        qhMapFilesEntity.setAffiliatedFileName(fileName);
        qhMapFilesEntity.setIsSave("0");
        qhMapFilesEntity.setIsDelete("0");
        qhMapFilesEntity.setType("RLMPolygon_");//类型
        qhMapFilesEntity.setPostfix("gdb");
        qhMapFilesEntity.setQualityStatus("1");//上传省市级类型（0：省，1：市）
        qhMapFilesEntity.setCodeType(codeType);//编码类型（0：UTF-8，1：GBK）
        qhMapFilesEntity.setDataLength(dataLength);//数据长度
        try {
            System.out.println(new Date()+"-------------------4.开始保存");
            //保存到上传文件管理表
            this.saveMapFiles(qhMapFilesEntity);
            System.out.println(new Date()+"-------------------5.结束保存");
            returnMsg.append("等待导入" + dataLength + "条数据.请第二天查询上传内容.");
            return returnMsg.toString();
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return "插入或更新数据出现错误: <br/>" + e.getMessage();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "数据导入失败, 服务器内部出现异常!";
        }
    }

    //保存GDB文件
    @Override
    public String newImportGDBFile(String filePath, String province, String city, String area, String createUser,String codeType,int dataLength) {
        // 返回的消息
        StringBuilder returnMsg = new StringBuilder();
        try {
            System.out.println(new Date()+"-------------------1.开始保存");
            //保存--zyt
            int count = qhRiskLevelMapPolygonImport.newSqlBatchSaveEntities_GDB(filePath,province,city,area,codeType,dataLength,createUser);
            if(count==dataLength){
                System.out.println(new Date()+"-------------------2.结束保存");
                returnMsg.append("成功导入" + count + "条数据.");
            }else{
                System.out.println(new Date()+"-------------------2.结束保存");
                returnMsg.append("导入出现异常，仅导入" + count + "条数据.");
            }
            return returnMsg.toString();
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            return "插入或更新数据出现错误: <br/>" + e.getMessage();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "数据导入失败, 服务器内部出现异常!";
        }
    }
}
